import { createRouter,createWebHistory } from "vue-router";


const router = createRouter({
    history:createWebHistory(),
    routes:[
        {
            path:'/',
            name:'home',
            redirect:'/rootUser',
            component:()=>import('../view/home.vue'),
            children:[
                {
                    path:'/data',
                    name:'data',
                    component:()=>import('../components/pc/Thisselect/data.vue')
                },
                {
                    path:'/rootUser',
                    name:'rootUser',
                    component:()=>import('../components/pc/Thisselect/rootUser.vue')
                },
                {
                    path:'/user',
                    name:'user',
                    component:()=>import('../components/pc/Thisselect/user.vue')
                },
                {
                    path:'/title',
                    name:'title',
                    component:()=>import('../components/pc/Thisselect/title.vue')
                },
                {
                    path:'/MaterialData',
                    name:'MaterialData',
                    component:()=>import('../components/pc/Thisselect/MaterialData.vue')
                },
                {
                    path:'/announcement',
                    name:'announcement',
                    component:()=>import('../components/pc/Thisselect/announcement.vue')
                },
            ]
        },
        {
            path:'/login',
            name:'login',
            component:()=>import('../view/login.vue')
        }
    ]
})

router.beforeEach(async(to, from, next) => {
    const token = localStorage.getItem('token'); // 假设token存储在localStorage中
    if (to.name !== 'Login' && !token) {
      // 如果用户尝试访问非登录页面且没有token，则重定向到登录页面
      next('/login');
    } else {
      // 否则，继续跳转
      next();
    }
  });


export default router